package com.amazon.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.amazon.shop.bean.dto.StoreForm;
import com.amazon.shop.bean.entity.RelUserStore;
import com.amazon.shop.bean.entity.Store;
import com.amazon.shop.bean.enums.ResultEnum;
import com.amazon.shop.bean.vo.PageVO;
import com.amazon.shop.bean.vo.Ret;
import com.amazon.shop.mapper.RelUserStoreMapper;
import com.amazon.shop.mapper.StoreMapper;
import com.amazon.shop.service.IRelUserStoreService;
import com.amazon.shop.service.IStoreService;
import com.amazon.shop.service.IUserService;
import com.amazon.shop.util.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzj
 * @since 2021-01-25
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    @Resource
    private IRelUserStoreService relUserStoreService;

    @Autowired
    private IUserService iUserService;

    @Override
    public Ret get(StoreForm storeForm) {
        Page<Store> page = new Page(storeForm.getCurrentNum(),storeForm.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        Integer ismanager = storeForm.getOismanager();
        if(ismanager!=null && ismanager==0){
            List<Integer> storeids = iUserService.listStoreidByUserid(storeForm.getOperatorUserid());
            if(storeids!=null&&storeids.size() > 0){
                queryWrapper.in("id",storeids);
            }else {
                return ResultUtil.success(new PageVO<>(page));
            }
        }
        if (StrUtil.isNotBlank(storeForm.getStorename())) {
            queryWrapper.eq("storename",storeForm.getStorename());
        }
        if (StrUtil.isNotBlank(storeForm.getEmail())) {
            queryWrapper.eq("email",storeForm.getEmail());
        }
        if (StrUtil.isNotBlank(storeForm.getMid())) {
            queryWrapper.eq("mid",storeForm.getMid());
        }
        page = this.page(page,queryWrapper);
        return ResultUtil.success(new PageVO<>(page));
    }

    @Override
    public Ret add(StoreForm storeForm) {
        String mid = storeForm.getMid();
        if (StrUtil.isBlank(mid)) {
            mid = storeForm.getStorename();
            storeForm.setMid(mid);
        }
        QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mid", mid);
        int count = this.count(queryWrapper);
        if(count>0){
            return ResultUtil.error(ResultEnum.EXISTERROR.getCode(),ResultEnum.EXISTERROR.getServecode(),"店铺网页识别码已存在，请换一个设置");
        }
        Store store = new Store();
        BeanUtil.copyProperties(storeForm,store);
        boolean b = this.save(store);
        if(b){
            RelUserStore relUserStore = new RelUserStore();
            relUserStore.setUserid(storeForm.getUserid());
            relUserStore.setStoreid(store.getId());
            relUserStoreService.save(relUserStore);
        }
        return b? ResultUtil.success():ResultUtil.error(ResultEnum.ADDERROR);
    }
}
